package com.lej.dataservice.club.http.coupons

import android.content.Context
import android.graphics.Point
import android.view.View
import android.view.ViewGroup
import android.view.ViewPropertyAnimator
import android.view.WindowManager
import android.view.animation.AccelerateInterpolator
import android.view.animation.DecelerateInterpolator
import android.view.animation.LinearInterpolator
import androidx.appcompat.app.AppCompatActivity
import androidx.lifecycle.lifecycleScope
import com.lej.core.R
import com.lej.core.base.currentActivity
import com.lej.core.base.homeActivity
import com.lej.core.databinding.LayoutCouponsPlusBinding
import com.lej.core.dsl.dp
import com.lej.core.utils.extention.dpf
import com.lej.core.utils.extention.safe
import kotlinx.coroutines.launch
import kotlin.coroutines.resume
import kotlin.coroutines.suspendCoroutine


object CouponsReward {
    private suspend fun View.animIt(block: ViewPropertyAnimator.() -> Unit) {
        suspendCoroutine<Boolean> {
            animate().setDuration(400).apply { block() }
                .withEndAction { it.resume(true) }.start()
        }
    }

    fun reward(bean: CouponsRewardPopBean) {
        (currentActivity as? AppCompatActivity)?.let {
            it.lifecycleScope.launch {
                kotlin.runCatching {
                    val targetDp = 300
                    val parent = it.window.decorView as ViewGroup
                    val view = it.layoutInflater.inflate(R.layout.layout_coupons_plus, parent, true)
                    val binding = LayoutCouponsPlusBinding.bind(view.findViewById(R.id.cl_coupons))
                    val goldNum = bean.gold.toIntOrNull().safe()
                    if (goldNum == 0) return@launch
                    binding.tvConsumeCoupons.text = "+${goldNum}"
                    binding.tvConsumeDesc.text = bean.title

                    binding.root.animIt {
                        interpolator = AccelerateInterpolator()
                        translationY((targetDp + 30).dpf)
                        duration = 300
                    }
                    binding.root.animIt {
                        interpolator = DecelerateInterpolator()
                        translationY((targetDp).dpf)
                        duration = 200
                    }
                    repeat(2) {
                        binding.root.animIt {
                            interpolator = AccelerateInterpolator()
                            scaleX(1.2f)
                            scaleY(1.2f)
                            duration = 300
                        }
                        binding.root.animIt {
                            interpolator = DecelerateInterpolator()
                            scaleX(0.9f)
                            scaleY(0.9f)
                            duration = 300
                        }
                    }
                    if (currentActivity?.javaClass?.simpleName == homeActivity?.simpleName) {
                        val wm = it.getSystemService(Context.WINDOW_SERVICE) as WindowManager
                        val size = Point().also { wm.defaultDisplay.getSize(it) }

                        val targetX = size.x/2f  - binding.root.measuredWidth/2
                        val targetY = size.y.toFloat()+ binding.root.measuredHeight/2- 40.dp

                        binding.root.animate().apply {
                            interpolator = LinearInterpolator()
                            translationX(targetX)
                            duration = 400
                        }.start()
                        binding.root.animIt {
                            interpolator = AccelerateInterpolator()
                            translationY(targetY)
                            duration = 400
                        }
                        binding.root.animIt {
                            interpolator = LinearInterpolator()
                            scaleX(0.3f)
                            scaleY(0.3f)
                            duration = 400
                        }
                        binding.root.animIt {
                            interpolator = AccelerateInterpolator()
                            scaleX(0f)
                            scaleY(0f)
                            alpha(0f)
                            duration = 200
                        }
                    } else {
                        binding.root.animIt {
                            interpolator = DecelerateInterpolator()
                            scaleX(1f)
                            scaleY(1f)
                            duration = 100
                        }
                        binding.root.animIt {
                            translationY((targetDp + 30).dpf)
                            duration = 150
                        }
                        binding.root.animIt {
                            interpolator = AccelerateInterpolator()
                            duration = 250
                            translationY(0f)
                        }
                    }
                    parent.removeView(binding.root)
                }
            }

        }
    }
}